package com.aoxon.cargo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.aoxon.cargo.R;
import com.aoxon.cargo.bean.GsonBean;
import com.aoxon.cargo.bean.Supplier;
import com.aoxon.cargo.loader.NormalDataLoader;
import com.aoxon.cargo.service.MyServiceFactory;
import com.aoxon.cargo.service.RegisterSupplierService;
import com.aoxon.cargo.shared.SupIndexShared;
import com.aoxon.cargo.util.CheckStateUtil;
import com.aoxon.cargo.util.DataUtil;
import com.aoxon.cargo.util.ToastUtil;

public class SupRegisterFastActivity extends BaseActivity implements
		NormalDataLoader.OnLoadDataListener {
	private EditText etPhone;
	private TextView tvRegister;
	private TextView tvTitle;
	private ImageView ivReturn;
	private RegisterSupplierService registerSupplierService = (RegisterSupplierService) MyServiceFactory
			.getInstance(MyServiceFactory.REGISTER_SUPPLIER);
	private NormalDataLoader normalDataLoader = new NormalDataLoader(this);
	private GsonBean gbSup;
	private Supplier supplier;
	
	private String tag = "SupRegisterFastActivity";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setup();
	}

	private void setup() {
		setContentView(R.layout.sup_register_fast);
		etPhone = (EditText) findViewById(R.id.etPhone_register_fast);
		tvRegister = (TextView) findViewById(R.id.tvRegister_register_fast);
		ivReturn = (ImageView) findViewById(R.id.ivPublicTitleReturn);
		tvTitle = (TextView) findViewById(R.id.tvPublicTiTle);
		tvRegister.setOnClickListener(onClick);
		ivReturn.setOnClickListener(onClick);
		tvTitle.setText("急速注册");
		normalDataLoader.setOnloadDataListener(this);
	}

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.tvRegister_register_fast:
				if (checkData()) {// 数据正确
					normalDataLoader.load();
				}else{
					ToastUtil.show(SupRegisterFastActivity.this, "请填写正确的手机号码");
				}
				break;
			case R.id.ivPublicTitleReturn:
				Intent intent = new Intent(getBaseContext(),
						SupLoginActivity.class);
				startActivity(intent);
				SupRegisterFastActivity.this.finish();
				break;

			}
		}

	};

	private boolean checkData() {
		if (!etPhone.getText().toString()
				.matches("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$")) {
			ToastUtil.show(this, "请填写正确的手机号码");
			return false;
		} else {
			return true;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(SupRegisterFastActivity.this,
					SupLoginActivity.class);
			startActivity(intent);
			this.finish();
			return false;
		}
		return false;
	}
	
	@Override
	public void beforeLoad() {
		// TODO Auto-generated method stub
		Log.v(tag, " before loag");
	}

	@Override
	public void toLoad(Message msg) throws Exception {
		// TODO Auto-generated method stub
		final Supplier supplier = new Supplier();
		supplier.setSupPhone(etPhone.getText().toString());
		supplier.setSupPassword("123456");
		gbSup = registerSupplierService.execute(supplier);
		Log.v(tag, "loading " + DataUtil.gson.toJson(gbSup));
	}

	@Override
	public void afterLoad(int state) {
		// TODO Auto-generated method stub
		Log.v(tag, " after load");
		try {
			if(CheckStateUtil.check(SupRegisterFastActivity.this, gbSup, "注册")){
				Intent intent = new Intent(SupRegisterFastActivity.this,
						SupRegisterSuccessActivity.class);
				Log.v(tag, gbSup.getStrJson());
				SupIndexShared.saveSupplierAndKey(gbSup.getStrJson());
				intent.putExtra("supplier", gbSup.getStrJson());
				startActivity(intent);
				finish();
			}else{
				Log.v(tag, "注册失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
